package cn.edu.ctbu.simplescoremanagementsystem.service.impl;

import cn.edu.ctbu.simplescoremanagementsystem.constain.ResultEnum;
import cn.edu.ctbu.simplescoremanagementsystem.domain.Student;
import cn.edu.ctbu.simplescoremanagementsystem.exception.ResultException;
import cn.edu.ctbu.simplescoremanagementsystem.repository.StudentRepository;
import cn.edu.ctbu.simplescoremanagementsystem.service.StudentService;
import cn.edu.ctbu.simplescoremanagementsystem.utils.ResultUtils;
import cn.edu.ctbu.simplescoremanagementsystem.vo.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.List;
@Slf4j
@Service
public class StudentServiceImpl implements StudentService {
    @Autowired
    StudentRepository studentRepository;
    public Result<List<Student>> findAll(){
        return ResultUtils.success(studentRepository.findAll());
    }

    public Result<Student> findById(Integer id){
        Student student=studentRepository.findById(id).orElse(null);
        return ResultUtils.success(student);
    }

    public Result<Student> insert(Student student){
        studentRepository.save(student);
        log.info("插入学生信息成功");
        return ResultUtils.success(student);
    }

    public Result<Student> update(Student student){
        studentRepository.save(student);
        log.info("更新学生信息成功");
        return ResultUtils.success(student);
    }

    public Result<Student> Delete(Integer id) throws Exception{
        Student student=studentRepository.findById(id).orElse(null);
        if(student == null){
            log.error("删除学生信息失败，该学生不存在");
            throw new ResultException(ResultEnum.NOT_FIND);
        }
        studentRepository.deleteById(id);
        log.info("删除学生信息成功");
        return ResultUtils.success(student);
    }

    public Page<Student> getByPage(Example<Student> student, Pageable pageable){
        Page<Student> page = studentRepository.findAll(student,pageable);
        return page;
    }

    public Page<Student> getByPage(Pageable pageable){
        Page<Student> page = studentRepository.findAll(pageable);
        return page;
    }

    public Student validateUsernameAndPassword(String username, String password) throws Exception{
        List<Student> students = studentRepository.findBySno(username);
        if (students.size() > 0) {
            //可能对password加密，但暂时我们不做处理
            Student student = students.get(0);
            if (student.getPassword().equals(password)) {
                return student;
            } else {
                throw new ResultException(ResultEnum.LOGIN_ERR);
            }
        }else{
            throw new ResultException(ResultEnum.LOGIN_ERR);
        }
    }

    public List<Student> findByNoAndPassword(String username, String password){
        return studentRepository.findBySnoAndPassword(username,password);
    }

}
